/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.fragments;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.cholm.media.castdr.R;
import org.cholm.media.castdr.adapters.CategoryAdapter;
import org.cholm.media.castdr.backend.Category;
import org.cholm.media.castdr.backend.Extractor;

import org.cholm.media.castdr.LogUtils; import static org.cholm.media.castdr.LogUtils.LOGd;
import static org.cholm.media.castdr.LogUtils.LOGe;

/**
 * A fragment that shows the list of sub-categories for each category.
 *
 * @author cholm
 */
public class CategoryFragment extends ListFragment {
    /**
     * Debug tag
     */
    private static final String TAG = LogUtils.makeLogTag(CategoryFragment.class);
    private IdSaver mSaver = new IdSaver(TAG);
    /**
     * The category
     */
    private Category mCategory = null;
    /**
     * The adapter
     */
    private CategoryAdapter mAdapter = null;
    /**
     * Listens to call-backs from sub-category
     */
    private OnSubCategoryListener mListener = null;

    // ===============================================================

    // ===============================================================
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mSaver.restore(savedInstanceState);
        }
    }

    /**
     * Create our view
     */
    public View onCreateView(LayoutInflater inflator, ViewGroup container,
                             Bundle savedInstance) {
        int id = mSaver.getId();
        LOGd(TAG, "Creating top view for category " + id);
        if (id < 0) {
            LOGe(TAG, "Arg! no valid id: " + id);
            return null;
        }
        mCategory = Extractor.instance().getList().get(id);
        LOGd(TAG, mCategory.getName());

        mAdapter = new CategoryAdapter(mCategory, getActivity());
        setListAdapter(mAdapter);

        LOGd(TAG, "Created fragment");

        View v = super.onCreateView(inflator, container, savedInstance);
        v.setBackgroundResource(R.drawable.right_border);
        // v.setBackgroundColor(Color.GREEN);
        return v;
    }

    @Override
    public void onDestroyView() {
        LOGd(TAG, "Destroying subFragment: " + mCategory.getName());
        super.onDestroyView();
        mSaver.save();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        LOGd(TAG, "Saving instance state " + mSaver.getId());
        mSaver.save(outState);
        super.onSaveInstanceState(outState);
    }

    /**
     * Handle item click
     */
    public void onListItemClick(ListView l, View v, int position, long id) {
        CategoryAdapter.Holder h = (CategoryAdapter.Holder) v.getTag();
        if (h != null) h.update(true);

        if (mListener != null) {
            mListener.onSubCategorySelect(position);
        }
    }

    /**
     * Set the listener
     *
     * @param l Implementation of OnSubCategoryListener
     */
    public void setListener(OnSubCategoryListener l) {
        mListener = l;
    }

    public void setCategoryId(int id) {
        mSaver.setId(id);
    }

    /**
     * The interface for call-backs when selecting a sub-category
     *
     * @author cholm
     */
    public interface OnSubCategoryListener {
        public void onSubCategorySelect(int s);
    }
}
// EOF
